package com.situ.lession1226.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.lession1226.dao.ReviewDAO;
import com.situ.lession1226.model.Review;
import com.situ.lession1226.service.ReviewService;
import com.situ.lession1226.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class ReviewServiceImpl implements ReviewService {
    private ReviewDAO dao;

    @Autowired
    public void setDao(ReviewDAO dao) {
        this.dao = dao;
    }

    @Override
    public List<Review> findAll(Integer id) {

        //2.正常进行查询
            List<Review> Reviews = dao.findAllReviewByNewsId(id);

            return Reviews;

    }


    @Override
    public List<Review> findAllBy(PaginateInfo pi, Review search) {
        //自动分页插件的操作
        //1.启动分页
        try (Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize());) {
            //2.正常进行查询
            List<Review> reviews = dao.findAllBy(search);

            //3.获取分页数据
            PageInfo<Review> pif = new PageInfo<>(reviews);
            pif.calcByNavigatePages(10);//根据导航页共10页进行计算
            pi.setPageInfo(pif);//将分页对象缓存

            return reviews;
        }
    }

//    @Override
//    public Review findById(Integer id) {
//        return dao.findById(id);
//    }
//
    @Override
    public int deleteByIds(Integer[] ids) {
        return dao.deleteByIds(ids);
    }

    @Override
    public boolean save(Review review) {
        return dao.save(review) > 0;
    }

    @Override
    public List<Review> findMy(PaginateInfo pi, Review review) {
        try (Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize());) {
            //2.正常进行查询
            List<Review> reviews = dao.findMy(review);

            //3.获取分页数据
            PageInfo<Review> pif = new PageInfo<>(reviews);
            pif.calcByNavigatePages(10);//根据导航页共10页进行计算
            pi.setPageInfo(pif);//将分页对象缓存

            return reviews;
        }
    }
//
//    @Override
//    public boolean update(Review review) {
//        return dao.update(review) > 0;
//    }
}